/*
 * test the insert and delete record function in one page
 * (serialize and deserialize)
 */

#include "inc.h"
#include "pager.h"
#include "util.h"

// #define TEST_BORROW_R
#define TEST_BOOK_R
// #define TEST_USER_R

int main()
{
    srand((unsigned int) time(NULL));

    pager_t *pager = createPager(USER);
    if (pager == NULL)
        exit(-1);

    page_t page;
    getPage(pager, &page);

#ifdef TEST_USER_R
    /* serialize */
    record_t record;
    while (1) {
        int id = rand() % 500;
        generateRecord(id, &record, USER);
        if (inseretRecordToPage(&page, &record) == -1) 
            break;
    }
    printf("\nAfter insert: \n");
    printPageLayout(&page);
    printSlot(&page);

    /* deserialize */
    printRecord(&page, USER);

    printf("\nAfter delete: \n");
    int count = 100;
    int delete_hit_nums = 0;
    while (count--) {
        int delete_index = rand() % 500;
        if (deleteRecordfromPage(&page, delete_index) == 0) {
            printf("delete: %d hit\n", delete_index);
            delete_hit_nums++;
        }
    }
    printf("total delete hit: %d\n", delete_hit_nums);
    printPageLayout(&page);
    printSlot(&page);

    /* deserialize */
    printRecord(&page, USER);

#endif /* TEST_USER_R */

#ifdef TEST_BOOK_R
    /* serialize */
    record_t record;
    while (1) {
        int id = rand() % 1000;
        generateRecord(id, &record, BOOK);
        if (inseretRecordToPage(&page, &record) == -1) 
            break;
    }
    printf("\nAfter insert: \n");
    printPageLayout(&page);
    printSlot(&page);
    
    /* deserialize */
    printRecord(&page, BOOK);

    printf("\nAfter delete: \n");
    int count = 100;
    int delete_hit_nums = 0;
    while (count--) {
        int delete_index = rand() % 500;
        if (deleteRecordfromPage(&page, delete_index) == 0) {
            printf("delete: %d hit\n", delete_index);
            delete_hit_nums++;
        }
    }
    printf("total delete hit: %d\n", delete_hit_nums);
    printPageLayout(&page);
    printSlot(&page);
    /* deserialize */
    printRecord(&page, BOOK);

#endif /* TEST_BOOK_R */

#ifdef TEST_BORROW_R
    /* serialize */
    record_t record;
    while (1) {
        int id = rand() % 1000;
        generateRecord(id, &record, BORROW);
        if (inseretRecordToPage(&page, &record) == -1) 
            break;
    }
    printf("\nAfter insert: \n");
    printPageLayout(&page);
    printSlot(&page);

    /* deserialize */
    printRecord(&page, BORROW);

    printf("\nAfter delete: \n");
    int count = 100;
    int delete_hit_nums = 0;
    while (count--) {
        int delete_index = rand() % 500;
        if (deleteRecordfromPage(&page, delete_index) == 0) {
            printf("delete: %d hit\n", delete_index);
            delete_hit_nums++;
        }
    }
    printf("total delete hit: %d\n", delete_hit_nums);
    printPageLayout(&page);
    printSlot(&page);
    /* deserialize */
    printRecord(&page, BORROW);

#endif /* TEST_BORROW_R */

    cleanPage(&page);
    destroyPager(pager);

    exit(0);
}